package com.lingshi.shopping_goods_service.service;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingshi.shopping.constant.Constant;
import com.lingshi.shopping.entity.*;
import com.lingshi.shopping.service.GoodsService;
import com.lingshi.shopping.service.SearchService;
import com.lingshi.shopping_goods_service.mapper.GoodsImageMapper;
import com.lingshi.shopping_goods_service.mapper.GoodsMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

import static com.lingshi.shopping.constant.Constant.DELETE_GOODS_TOPIC;

@DubboService
@Transactional
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsImageMapper goodsImageMapper;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public void add(Goods goods) {
        /**
         * 新增数据表
         * 1，商品基本数据表 t_goods
         * 2，商品图片表  t_goods_image
         * 3，商品规格数据表 t_goods_specification_option
         */

        long goodsId = IdUtil.getSnowflakeNextId();
        goods.setId(goodsId);

        goodsMapper.insert(goods);

        goods.getImages().forEach(image -> {
            image.setGoodsId(goodsId);
            goodsImageMapper.insert(image);
        });

        //获取商品规格
        List<Specification> specifications = goods.getSpecifications();

        //获取所有规格项
        for (Specification specification : specifications) {
            //获取规格项
            specification.getSpecificationOptions().forEach(option -> {
                goodsMapper.addGoodsSpecificationOption(goodsId, option.getId());
            });
        }

        //==========同步到ES===========
        syncGoodsToEs(goods.getId());
    }


    @Override
    public void update(Goods goods) {
        /**
         * 修改数据表
         * 1，商品基本数据表 t_goods
         * 2，商品图片表  t_goods_image
         *  2.1 先删除之前的所有此商品相关的图片
         * 3，商品规格数据表 t_goods_specification_option
         * 3.1  先删除之前的所有此商品相关的规格数据
         */


        goodsMapper.updateById(goods);


        LambdaUpdateWrapper<GoodsImage> imageLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        imageLambdaUpdateWrapper.eq(GoodsImage::getGoodsId, goods.getId());
        goodsImageMapper.delete(imageLambdaUpdateWrapper);

        //TODO 删除OSS 服务图片

        goods.getImages().forEach(image -> {
            image.setGoodsId(goods.getId());
            goodsImageMapper.insert(image);
        });


        //删除规格数据
        goodsMapper.deleteGoodsSpecificationOption(goods.getId());

        //获取商品规格
        List<Specification> specifications = goods.getSpecifications();

        //获取所有规格项
        for (Specification specification : specifications) {
            //获取规格项
            specification.getSpecificationOptions().forEach(option -> {
                goodsMapper.addGoodsSpecificationOption(goods.getId(), option.getId());
            });
        }
        //==========同步到ES===========
        syncGoodsToEs(goods.getId());
    }

    @Override
    public Goods findById(Long id) {

        Goods goods = goodsMapper.selectByPrimaryKey(id);

        return goods;
    }

    @Override
    public void putAway(Long id, Boolean isMarketable) {
        Goods goods = new Goods();
        goods.setId(id);
        goods.setIsMarketable(isMarketable);
        goodsMapper.updateById(goods);
        if(isMarketable){
            syncGoodsToEs(id);
        }else {
            rocketMQTemplate.syncSend(DELETE_GOODS_TOPIC,id);
        }
    }

    /**
     * 同步商品到es
     * @param goodsId
     */
    private void syncGoodsToEs(Long goodsId ) {
        GoodsDesc goodsDesc = this.findDesc(goodsId);
        rocketMQTemplate.syncSend(Constant.SYNC_GOODS_TOPIC, goodsDesc);
    }

    @Override
    public Page<Goods> search(String goodsName, int page, int size) {
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(StrUtil.isNotBlank(goodsName), Goods::getGoodsName, goodsName);

        Page<Goods> pageObj = new Page<>(page, size);

        goodsMapper.selectPage(pageObj, queryWrapper);

        return pageObj;
    }

    @Override
    public List<GoodsDesc> findAll() {
        return goodsMapper.findAll();
    }

    @Override
    public GoodsDesc findDesc(Long id) {
        return goodsMapper.findDesc(id);
    }
}
